package com.lanou.musicbox.fragment.onlinemusic;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.onlinemusic.SingersListAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.bean.Singer;
import com.lanou.musicbox.utils.Contant;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by 柳柏庆 on 15/10/21.
 * 网络音乐_歌手页面
 */
public class SingersFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private Context context;
    private SingersListAdapter adapter;
    private ListView listView;
    private VolleySingleton volleySingleton;
    private ArrayList<Singer.DataEntity> dataEntities=new ArrayList<>();
    private StringRequest stringRequest;
    private RadioButton singer_frist, singer_sencond, singer_three, singer_four, singer_five;//数列的参数
    private Button singer_item_name;//中国歌手前100名参数
    private RadioButton singer_man, singer_women, singer_list;//男歌手 女歌手 组合属性
    private RadioGroup linearlayout_sex;
    private View view;
    private String country = "country";
    private String sex = "sex";
    private boolean isFirst = false;
   private FragmentManager fragmentManager;//定义个碎片管理化属性
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.singers_list, null);
        country="c";
        sex="m";
        context = getActivity();

        //初始化（参数是当前类对象中的参数）
        initdata(Contant.Url.URL_TOP);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listView= (ListView) view.findViewById(R.id.singer_listview);
        listView.setOnItemClickListener(this);
        //歌手TOP100
        singer_frist = (RadioButton) view.findViewById(R.id.singer_frist);
        singer_frist.setOnClickListener(this);
        //最近很红
        singer_sencond = (RadioButton) view.findViewById(R.id.singer_sencond);
        singer_sencond.setOnClickListener(this);
        //华语
        singer_three = (RadioButton) view.findViewById(R.id.singer_three);
        singer_three.setOnClickListener(this);
        //欧美
        singer_four = (RadioButton) view.findViewById(R.id.singer_four);
        singer_four.setOnClickListener(this);
        //日韩
        singer_five = (RadioButton) view.findViewById(R.id.singer_five);
        singer_five.setOnClickListener(this);
        //男歌手
        singer_man = (RadioButton) view.findViewById(R.id.singer_man);
        singer_man.setOnClickListener(this);
        //女歌手
        singer_women = (RadioButton) view.findViewById(R.id.singer_woman);
        singer_women.setOnClickListener(this);
        //组合
        singer_list = (RadioButton) view.findViewById(R.id.singer_list);
        singer_list.setOnClickListener(this);
        singer_item_name = (Button) view.findViewById(R.id.singer_item_name);
        singer_item_name.setOnClickListener(this);
      linearlayout_sex= (RadioGroup) view.findViewById(R.id.linearlayout_sex);

    }
      /**
       * 创建构造方法用来解析数据
       * */
    public void initdata(String url) {
        volleySingleton = VolleySingleton.getInstance(context);
        stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Singer singer = gson.fromJson(response, Singer.class);
                if (singer != null && singer.getData() != null) {
                    dataEntities =(ArrayList<Singer.DataEntity>) singer.getData();

                }
                adapter=new SingersListAdapter(getActivity(),dataEntities);
                listView.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
         volleySingleton.addQueue(stringRequest);


    }
          private void overall(){
            singer_item_name.setVisibility(View.GONE);//隐藏不需要的布局
            linearlayout_sex.setVisibility(View.VISIBLE);//调用当前显示的
              switch (country+sex){
                  case "cm"://华语男歌手
                      initdata(Contant.Url.URL_CHINESEBOY);
                      break;
                  case "cw"://华语女歌手
                      initdata(Contant.Url.URL_CHINESEGIRL);
                      break;
                  case "cl"://华语组合
                      initdata(Contant.Url.URL_CHINESE);
                      break;
                  case "om"://欧美男歌手
                      initdata(Contant.Url.URL_OCCIDENTBOY);
                      break;
                  case "ow"://欧美女组合
                      initdata(Contant.Url.URL_OCCIDENTGIRL);
                      break;
                  case "ol"://欧美组合
                      initdata(Contant.Url.URL_OCCIDENT);
                      break;
                  case "jm"://日本男歌手
                      initdata(Contant.Url.URL_JKBOY);
                      break;
                  case "jw"://日本女歌手
                      initdata(Contant.Url.URL_JKGIRL);
                      break;
                  case "jl"://日本组合
                      initdata(Contant.Url.URL_JK);
                      break;
              }
          }

    @Override
    public void onClick(View v) {
    switch (v.getId()){
        case R.id.singer_frist:
            initdata(Contant.Url.URL_TOP);
            singer_item_name.setVisibility(View.VISIBLE);
            singer_item_name.setText("中国歌手前100名");
            linearlayout_sex.setVisibility(View.GONE);
          break;
        case R.id.singer_sencond:
            initdata(Contant.Url.URL_RECENTLY);
            singer_item_name.setVisibility(View.VISIBLE);
            singer_item_name.setText("一周内上升最快的歌手");
            linearlayout_sex.setVisibility(View.GONE);
            break;
        case R.id.singer_three:

            country="c";
            overall();
            break;
        case R.id.singer_four:

            country="o";
            overall();
            break;
        case R.id.singer_five:

            country="j";
            overall();
            break;
        case R.id.singer_man:

            sex="m";
            overall();
            break;
        case R.id.singer_woman:
            sex="w";
            overall();
            break;
        case R.id.singer_list:
            sex="l";
            overall();
            break;
    }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        /**
         * 通过bundle进行传值
         * **/
        EnterSingersFragment fragment =new EnterSingersFragment();
        fragmentManager= getActivity().getSupportFragmentManager();
        Bundle bundle =new Bundle();
        bundle.putString("songs_id", adapter.getUrl(position) + "");
        bundle.putString("singer_img", dataEntities.get(position).getPic_url());
        bundle.putString("name", dataEntities.get(position).getSinger_name());
        fragment.setArguments(bundle);
        FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fill_main,fragment).addToBackStack(null).commit();

        Log.i("22222222",adapter.getUrl(position)+" ");
    }
}
